import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/input_item_sms_code.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/toggle_default_item.dart';
import 'package:xinxuan/controllers/my_controller/bank_card_controller.dart';
import 'package:xinxuan/utils/color_util.dart';

class AddReceivingBankCard extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BankCardController>(
      init: BankCardController(),
      builder: (controller) {
        return Column(
          children: [
            Expanded(child: ScrollConfigurationCommon(child: ListView(
              children: [
                InputItem(
                  title: '账号名', 
                  hintText: '请填写账户名', 
                  defaultValue: '${controller.accountName.value}', 
                  onChenge: (String value) {
                    controller.accountName.value = value;
                  }
                ),
                InputItem(
                  title: '账号', 
                  hintText: '请填写银行或支付宝账号', 
                  defaultValue: '${controller.cardNumber.value}', 
                  keyBoardType: TextInputType.number, 
                  onChenge: (String value) {
                    controller.cardNumber.value = value;
                  }
                ),
                _bankListItem(controller),
                InputItem(
                  title: '身份证号', 
                  hintText: '请输入身份证号码', 
                  defaultValue: '${controller.identityNumber.value}', 
                  keyBoardType: TextInputType.number, 
                  onChenge: (String value) {
                    controller.identityNumber.value = value;
                  }
                ),
                Obx(() => InputItemSmsCode(
                  defaultValue: controller.verificationCode.value,
                  duration: controller.duration.value,
                  value: controller.verificationCode.value,
                  onChenge: (String value) {
                    controller.verificationCode.value = value;
                  }, 
                  onTap: () {
                    if (controller.duration.value >= 60) {
                      controller.getSmsCode();
                    }
                  }
                )),
                ToggleDefaultItem(
                  title: '设为默认卡片', 
                  value: controller.isDefault.value == 1, 
                  showBorder: false,
                  onToggle: (bool value) {
                    controller.isDefault.value = value ? 1 : 0;
                    controller.update();
                  }
                ),
              ],
            ))),
            Container(
              padding: EdgeInsets.only(left: 12.w, right: 12.w, bottom: ScreenUtil().bottomBarHeight),
              color: bgWhiteColor,
              height: 56.h + ScreenUtil().bottomBarHeight,
              alignment: Alignment.center,
              child: ButtonUtil(
                text: '保存', 
                fontSize: 16.sp, 
                borderRadius: 30.w, 
                height: 36.h, 
                onTop: () {
                  controller.addBankCard();
                }
              ),
            )
          ],
        );
      },
    );
  }

  /// 银行卡下拉项
  Widget _bankListItem(BankCardController controller) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        height: 54.h,
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                '所属银行',
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            Expanded(
              child: InkWellUtil.shakeProofInkWell(Row(
                children: [
                  Expanded(child: Container(
                    child: Text(
                      controller.selectedBankItem.value.title == null ? '请选择开户行' : '${controller.selectedBankItem.value.title}',
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    ),
                  )),
                  Image.asset(
                    'assets/icons/icon_arrow_right.png',
                    width: 15.w,
                    height: 15.w,
                  )
                ],
              ), () {
                Get.bottomSheet(Container(
                  height: 300.h,
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
                  ),
                  child: Column(
                    children: [
                      Container(
                        height: 54.h,
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            InkWellUtil.shakeProofInkWell(Text(
                              '取消',
                              style: TextStyle(
                                color: textSubtitleColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              Get.back();
                            }),
                            InkWellUtil.shakeProofInkWell(Text(
                              '确认',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 16.sp
                              ),
                            ), () {
                              controller.determineBank();
                              Get.back();
                            })
                          ],
                        ),
                      ),
                      Expanded(
                        child: ScrollConfigurationCommon(child: CupertinoPicker(
                          scrollController: controller.scrollController.value,
                          diameterRatio: 5.w,
                          itemExtent: 54.h, 
                          onSelectedItemChanged: (int index) {
                            controller.changeBankIndex(index);
                          }, 
                          children: controller.systemBankArr.map((item) {
                            return Container(
                              height: 54.h,
                              alignment: Alignment.center,
                              child: Text(
                                '${item.title}',
                                style: TextStyle(
                                  color: textBlackColor,
                                  fontSize: 16.sp
                                ),
                              ),
                            );
                          }).toList()
                        )),
                      )
                    ],
                  ),
                ));
              }),
            )
          ],
        ),
      ),
    );
  }
}